<?php
require_once ('./Model/RegModel.php');
require_once ('./View/RegView.php');
require_once ('./Model/Validation.php');
require_once ('./Model/DBSettings.php');
require_once ('./Static/Messages.php');
/*
 * Kontrollerar och loggar in och ut användare
 * Visar relevanta meddelanden
 * 
 * @retun string HTML
 */

class RegController {
	
	public function DoControll(){
		
		$db = new mysqli(DBSettings::$host, DBSettings::$user, DBSettings::$password, DBSettings::$database);
		$regModel = new RegModel($db);
		$regView = new RegView();
		$validation = new Validation();
		
		//Validerar data som ska skickas til ldatabas
		if($regView->TriedToRegister()){
			if(!$validation->checkUsername($regView->GetUsername())){
				return $regView->DoRegisterBox(Messages::ERROR_USERNAME_INVALID, true);
			}
			if(!$validation->checkPassword($regView->GetPassword(), $regView->GetControllPassword())){
				return $regView->DoRegisterBox(Messages::ERROR_PASSWORD_INVALID, true);
			}
			if(!$regModel->UsernameExist($regView->GetUsername())){
				return $regView->DoRegisterBox(Messages::ERROR_USERNAME_EXIST, true);
			}	
			if($regModel->DoRegister($regView->GetUsername(), $regView->GetPassword()))	{
				return $regView->DoRegisterBox(Messages::SUCCESS_REGISTRATION_COMPLETE);
			}	
		}
		return $regView->DoRegisterBox();
	}
}
?>